<?php
namespace Admin\Controller;
use Think\Page;

class AgencyController extends AdminController {
	public function _initialize(){
		parent::_initialize();
	}
	
	/**
	 * 会员列表
	 */
	public function index(){
		$username = I('username');
		$admin_id=I('id');
		if(!empty($username)){
			$where['username'] = array('like','%'.$username.'%');
		}
		if (!empty($admin_id)){
			$where['admin_id']=$admin_id;
		}	
		$where['level']=2;	
		$count      =  M('Admin')->where($where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		//给分页传参数
		setPageParameter($Page, array('admin_id'=>$admin_id,'username'=>$username));
		$show       = $Page->show();// 分页显示输出
		$list =  M('Admin')
		->where($where)
		->order(" admin_id desc ")
		->limit($Page->firstRow.','.$Page->listRows)
		->select();	
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display(); // 输出模板
	}
	
		public function pay(){
			if(IS_GET){
				$username=I('username');
				$admin_id=I('id');
				
				if(!empty($admin_id)){
					$where['admin_id']=$admin_id;
				}
				if(!empty($username)){
					$where['user_name'] = array('like','%'.$username.'%');					
				}
				$count =  M('Admin_pay')->where($where)->count();// 查询满足要求的总记录数
				$Page  = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
				//给分页传参数
				setPageParameter($Page, array('admin_id'=>$admin_id,'username'=>$username));
				 
				$show       = $Page->show();// 分页显示输出
				 
				$list= M('Admin_pay')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('add_time desc')->select();			
				$this->assign('page',$show);
				$this->assign('list',$list);
				$this->display();
			}
		}
		public function recharge(){
			if(IS_POST){
			$money=I('money');
			$admin_id=I('admin_id');
			//dump($money);dump($admin_id);
			$admin=M('admin')->where('admin_id ='.$admin_id)->find();
			if($admin['level']!=2){
				$this->error('代理商级别不正确');
			}
			$data['admin_id']=$admin_id;
			$data['user_name']=$admin['username'];
			$data['add_time']=time();
			$data['money']=$money;
			$r[]=M('admin')->where('admin_id ='.$admin_id)->setInc('money',$money);
			$r[]=M('Admin_pay')->add($data);
					if($r){
						$this->success('添加成功');
					}else{
						$this->error('服务器繁忙，请稍后');
					}
			       }
		}	

}